package com.xiaoyu.lanling.feature.board.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.board.BoardTop3Event
import com.xiaoyu.lanling.event.board.GoddessCheckChangeEvent
import com.xiaoyu.lanling.feature.board.controller.BoardController
import com.xiaoyu.lanling.feature.board.data.BoardDataCreator
import com.xiaoyu.lanling.feature.board.model.BoardItem
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.OnShowFragment
import com.xiaoyu.lanling.view.UserAvatarDraweeView
import kotlinx.android.synthetic.main.fragment_goddess_summed_board.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class GoddessSummedBoardFragment : OnShowFragment() {
    companion object {
        const val INDEX_GOLD = 0
        const val INDEX_SILVER = 1
        const val INDEX_BRONZE = 2
    }

    private val requestTag = Any()
    private lateinit var controller: BoardController

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_goddess_summed_board, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        initData()
        initBind()
        initEvent()
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: GoddessCheckChangeEvent) {
                if (event.checkedIndex == 0) {
                    board_tab_view.setBackgroundResource(R.drawable.board_week_selected_background)
                } else {
                    board_tab_view.setBackgroundResource(R.drawable.board_sum_selected_background)
                }
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: BoardTop3Event) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }
                updateTop3Avatar(event.list)
            }
        })
    }

    fun initBind() {
        week_board.setOnClickListener {
            GoddessCheckChangeEvent(0).post()
        }

        sum_board.setOnClickListener {
            GoddessCheckChangeEvent(1).post()
        }

        val avatars = listOf(gold_avatar, silver_avatar, bronze_avatar)
        for (avatar in avatars) {
            avatar.setOnClickListener {
                val boardItem = it.getViewTag<BoardItem>() ?: return@setOnClickListener
                val activity = App.getInstance().topActivity ?: return@setOnClickListener
                Router.instance.gotoUserActivity(activity, boardItem.user)
            }
        }
    }

    private fun updateTop3Avatar(list: List<BoardItem>) {
        for ((i, item) in list.withIndex()) {
            when (i) {
                INDEX_GOLD -> displayTopUser(item, gold_avatar, gold_name, gold_index)
                INDEX_SILVER -> displayTopUser(item, silver_avatar, silver_name, silver_index)
                INDEX_BRONZE -> displayTopUser(item, bronze_avatar, bronze_name, bronze_index)
            }
        }
    }

    private fun displayTopUser(boardItem: BoardItem, avatar: UserAvatarDraweeView, name: TextView, index: TextView) {
        ImageViewUtil.loadImage(avatar, boardItem.loadParam)
        name.text = boardItem.user.name
        index.text = boardItem.index
        avatar.setViewTag(boardItem)
    }

    private fun initData() {
        context?.let {
            val listData = BoardDataCreator.createGoddessSummedBoardListData(requestTag)
            controller = BoardController.create(it, listData, swipe_refresh_layout, board_recyclerview, board_empty_hint_layout)
            controller.loadFirstPageData()
        }
    }
}